package basic.datastruct.List;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/24 18:16
 * @描述 暂无
 */
public class ListRemoveRepeatValue {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("e");
        System.out.println((Deduplication1(list)));
        System.out.println("---------");
        Deduplication2(list);
        System.out.println("---------");
        Deduplication3(list);
        System.out.println("---------");
    }

    /**
     *
     * @param list 任意
     * @param <T>   T需重写equals与hashCode方法！！
     * @return 去重List
     */
    public static <T> List<T> Deduplication1(List<T> list) {
        System.out.println("list = " + list );
        List<T> newList = new ArrayList<>();
        for(T t : list) {
            if(!newList.contains(t)) {
                newList.add(t);
            }
        }
        System.out.println("newList = " + newList);
        return newList;
    }

    public static void Deduplication2(List<String> list) {
        System.out.println("list = " + list );
        List<String> newList = new ArrayList<String>();
        Set set = new HashSet();
        for(String str : list) {
            if(set.add(str)) {
                newList.add(str);
            }
        }
        System.out.println("newList = " + newList);
    }

    // Set去重     由于Set的无序性，不会保持原来顺序
    public static void Deduplication3(List<String> list) {
        System.out.println("list = " + list );
        Set set = new HashSet();
        List<String> newList = new ArrayList<String>();
        set.addAll(list);
        newList.addAll(set);
        System.out.println("newList = " + newList);
    }




}
